﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuanLyVanBanBUS
{
    public class MauBieuBUS
    {
        public void themMauBieu(MauBieu mbDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            storeDB.MauBieus.InsertOnSubmit(mbDTO);

            storeDB.SubmitChanges();
        }

        public IList<MauBieu> layDanhSachMauBieuDaDuyet()
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.MauBieus.Where(q => q.NgayKiemSoat != null && q.MaNguoiKiemSoat != null).OrderByDescending(q => q.NgayKiemSoat).ToList();

            return query;
        }

        public IList<MauBieu> layDanhSachMauBieuChuaDuyetTheoMaPhong(string maPhong)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            //var query = storeDB.MauBieus.Where(q => q.NgayKiemSoat == null && q.MaNguoiKiemSoat == null).OrderByDescending(q => q.NgayKiemSoat).ToList();

            var query = (from mb in storeDB.MauBieus
                            join nd in storeDB.NguoiDungs on mb.MaNguoiSoanThao equals nd.MaNguoiDung
                            where mb.NgayKiemSoat == null & mb.MaNguoiKiemSoat == null && nd.MaPhong == maPhong
                            select mb).OrderByDescending(q => q.NgayKiemSoat).ToList();

            return query;
        }

        public IList<MauBieu> layDanhSachMauBieuDaDuyetTheoTenFileMauBieu(MauBieu mbDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.MauBieus.Where(q => q.TenFileMauBieu.Contains(mbDTO.TenFileMauBieu) && q.MaNguoiKiemSoat != null  && q.NgayKiemSoat != null).OrderByDescending(q => q.NgayKiemSoat).ToList();

            return query;
        }

        public IList<MauBieu> layDanhSachMauBieuChuaDuyetTheoTenFileMauBieu(MauBieu mbDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.MauBieus.Where(q => q.TenFileMauBieu.Contains(mbDTO.TenFileMauBieu) && q.MaNguoiKiemSoat == null && q.NgayKiemSoat == null).OrderByDescending(q => q.NgayKiemSoat).ToList();

            return query;
        }

        public IList<MauBieu> layDanhSachMauBieuChuaDuyetTheoTenFileMauBieuVaMaNguoiSoanThao(MauBieu mbDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.MauBieus.Where(q => q.TenFileMauBieu.Contains(mbDTO.TenFileMauBieu) && q.MaNguoiKiemSoat == null && q.NgayKiemSoat == null && q.MaNguoiSoanThao == mbDTO.MaNguoiSoanThao).OrderByDescending(q => q.NgayKiemSoat).ToList();

            return query;
        }

        public void duyetSoanThaoMauBieu(MauBieu mbDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.MauBieus.Where(q => q.NgayKiemSoat == null && q.MaMauBieu == mbDTO.MaMauBieu).First();
            query.NgayKiemSoat = DateTime.Now;
            query.MaNguoiKiemSoat = mbDTO.MaNguoiKiemSoat;

            storeDB.SubmitChanges();
        }

        public IList<MauBieu> layDanhSachMauBieuDaDuyetTheoNoiDungMauBieu(MauBieu mbDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.MauBieus.Where(q => q.NoiDungMauBieu.Contains(mbDTO.NoiDungMauBieu) && q.MaNguoiKiemSoat != null && q.NgayKiemSoat != null).OrderByDescending(q => q.NgayKiemSoat).ToList();

            return query;
        }

        public void suaMauBieuChuaDuyet(MauBieu mbDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.MauBieus.Where(q => q.MaMauBieu == mbDTO.MaMauBieu).First();

            query.NoiDungMauBieu = mbDTO.NoiDungMauBieu;
            query.TenFileMauBieu = mbDTO.TenFileMauBieu;
            query.TenFileCongVan = mbDTO.TenFileCongVan;
            query.MaNoiPhatHanh = mbDTO.MaNoiPhatHanh;
            query.TenDonViSoanThao = mbDTO.TenDonViSoanThao;

            storeDB.SubmitChanges();
        }

        public IList<MauBieu> layThongTinMauBieuTheoMaMauBieu(MauBieu mbDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.MauBieus.Where(q => q.MaMauBieu == mbDTO.MaMauBieu).ToList();

            return query;
        }
    }
}
